home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet multimedia / Animacje, filmy i prezentacje / Modelowanie 3D / K-3D 0.6.5.0 / k3d-all-in-one-setup-0.6.5.0.exe / k3d-setup-0.6.5.0.exe / share / shaders / k3d_oak.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-19  |  3.3 KB  |  92 lines

  1. /****************************************************************************
  2.  * oak.h - contains the oaktexture function, which is common to many
  3.  *         shader which make wood grain patterns.  It looks very much
  4.  *         like oak, and can also pass for ...
  5.  *
  6.  ***************************************************************************
  7.  *
  8.  * Author: Larry Gritz, 1999
  9.  *
  10.  * Contacts:  lg@pixar.com
  11.  *
  12.  * $Revision: 1.1 $    $Date: 2004/05/19 18:15:19 $
  13.  *
  14.  ****************************************************************************/
  15.  
  16. #include "k3d_noises.h"
  17.  
  18.  
  19. /* oaktexture -- generate a pattern much like the rings and grain of
  20.  *    oak wood.  Rings are centered on the z axis, so the caller should
  21.  *    position the space of Pshad so that the grain goes the way you want.
  22.  *    Note: this function takes derivatives, so it shouldn't be called 
  23.  *    from within varying conditionals or loops.
  24.  * Inputs:
  25.  *    Pshad, dPshad - sample point & its derivative
  26.  *    ringfreq - mean frequency of ring spacing
  27.  *    ringunevenness - 0=equally spaced rings, larger is unequally spaced
  28.  *    grainfreq - frequency of the fine grain
  29.  *    ringnoise, ringnoisefreq - general warping of the domain
  30.  *    trunkwobble, trunkwobblefreq - controls noise which wobbles the
  31.  *        axis of the trunk so that it's not perfectly on the z axis.
  32.  *    angularwobble, angularwobblefreq - warping indexed by angle about
  33.  *        the z axis.
  34.  *    ringy, grainy - overall scale on the degree to which rings and
  35.  *        grain are weighted.  0 turns one off, 1 makes full effect.
  36.  * Return value: 0 when in the "background" light wood, 1 when in the
  37.  *    darkest part of a ring or grain.
  38.  */
  39. float
  40. oaktexture (point Pshad;  float dPshad;
  41.         float ringfreq, ringunevenness, grainfreq;
  42.         float ringnoise, ringnoisefreq;
  43.         float trunkwobble, trunkwobblefreq;
  44.         float angularwobble, angularwobblefreq;
  45.         float ringy, grainy;)
  46. {
  47.     /* We shade based on Pshad, but we add several layers of warping: */
  48.     /* Some general warping of the domain */
  49.     vector offset = vfBm(Pshad*ringnoisefreq,dPshad*ringnoisefreq, 2, 4, 0.5);
  50.     point Pring = Pshad + ringnoise*offset;
  51.     /* The trunk isn't totally steady xy as you go up in z */
  52.     Pring += trunkwobble * 
  53.                vsnoise(zcomp(Pshad)*trunkwobblefreq) * vector(1,1,0);
  54.  
  55.     /* Calculate the radius from the center. */
  56.     float r2 = sqr(xcomp(Pring)) + sqr(ycomp(Pring));
  57.     float r = sqrt(r2) * ringfreq;
  58.     /* Add some noise around the trunk */
  59.     r += angularwobble * smoothstep(0,5,r)
  60.        * snoise (angularwobblefreq*(Pring)*vector(1,1,0.1));
  61.  
  62.     /* Now add some noise so all rings are not equal width */
  63.     extern float du, dv;
  64.     float dr = filterwidth(r);
  65.     r += ringunevenness*filteredsnoise(r,dr);
  66.  
  67.     float inring = smoothpulsetrain (.1, .55, .7, .95, 1, r);
  68.  
  69.     point Pgrain = Pshad*grainfreq*vector(1,1,.05);
  70.     float dPgrain = filterwidthp(Pgrain);
  71.     float grain = 0;
  72.     float i, amp=1;
  73.     for (i = 0;  i < 2;  i += 1) {
  74.     float grain1valid = 1-smoothstep(.2,.6,dPgrain);
  75.     if (grain1valid > 0) {
  76.         float g = grain1valid * snoise (Pgrain);
  77.         g *= (0.3 + 0.7*inring);
  78.         g = pow(clamp(0.8 - (g),0,1),2);
  79.         g = grainy * smoothstep (0.5, 1, g);
  80.         if (i == 0)
  81.         inring *= (1-0.4*grain1valid);
  82.         grain = max (grain, g);
  83.     }
  84.     Pgrain *= 2;
  85.     dPgrain *= 2;
  86.     amp *= 0.5;
  87.     }
  88.  
  89.     return mix (inring*ringy, 1, grain);
  90. }
  91.  
  92.